package bll

import (
	"context"

	"manager/event"
	"manager/model"
	"manager/model/entity"
	"manager/store"
	"manager/store/postgres"
	"time"

	"manager/auth"
)

type fault struct {
	iFault store.IFault
}

var Fault = &fault{
	iFault: postgres.Fault,
}

func (a *fault) init() func() {
	return func() {}
}

func (a *fault) onEvent(*event.Data) {}

// Create 创建
func (a *fault) Create(ctx context.Context, in *model.FaultCreateRequest) error {
	var (
		err error
	)

	// 获取用户Id
	in.UserId, _ = auth.ContextUserID(ctx)

	// 构建创建现场数据
	c := buildFault(in)
	_, err = a.iFault.Create(ctx, c)
	return err
}

// Update 更新
func (a *fault) Update(ctx context.Context, in *model.FaultUpdateRequest) error {
	var (
		dict = make(map[string]interface{})
	)

	updateAt := time.Now().Unix()
	in.UpdatedAt = &updateAt

	if in.UserId != nil {
		dict["user_id"] = in.UserId
	}

	if in.EbikeId != nil {
		dict["ebike_id"] = in.EbikeId
	}

	if in.EbikeName != nil {
		dict["ebike_name"] = in.EbikeName
	}

	if in.EbikeTypeId != nil {
		dict["ebike_type_id"] = in.EbikeTypeId
	}

	if in.EbikeTypeName != nil {
		dict["ebike_type_name"] = in.EbikeTypeName
	}

	if in.Phone != nil {
		dict["phone"] = in.Phone
	}

	if in.FaultId != nil {
		dict["fault_id"] = in.FaultId
	}

	if in.FaultName != nil {
		dict["fault_name"] = in.FaultName
	}

	if in.FaultDesc != nil {
		dict["fault_desc"] = in.FaultDesc
	}

	if in.FaultEnum != nil {
		dict["fault_enum"] = in.FaultEnum
	}

	if in.Level != nil {
		dict["level"] = in.Level
	}

	if in.SnCode != nil {
		dict["sn_code"] = in.SnCode
	}

	if in.UpdatedAt != nil {
		dict["updated_at"] = in.UpdatedAt
	}

	// do other update here
	return a.iFault.Update(ctx, in.Id, dict)
}

// Delete 删除
func (a *fault) Delete(ctx context.Context, in *model.FaultDeleteRequest) error {
	return a.iFault.Delete(ctx, in.Id)
}

// FaultTimes 故障次数
func (a *fault) FaultTimes(ctx context.Context, ebikeId int64) (count int64) {
	postgres.GetDB(ctx).Model(&entity.Fault{}).Where("ebike_id = ?", ebikeId).Count(&count)
	return
}

// List 列表查询
func (a *fault) List(ctx context.Context, in *model.FaultListRequest) (*model.FaultListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.Fault
		out   = &model.FaultListResponse{}
	)

	if total, list, err = a.iFault.List(ctx, in); err != nil {
		return nil, err
	}

	out.Total = total
	out.List = model.FaultsEntityToDto(list)

	return out, nil
}

// Find 列表查询
func (a *fault) Find(ctx context.Context, in *model.FaultInfoRequest) (*model.FaultInfo, error) {
	var (
		err  error
		data *entity.Fault
		out  = &model.FaultInfo{}
	)

	if data, err = a.iFault.Find(ctx, in); err != nil {
		return nil, err
	}

	out = model.FaultEntityToDto(data)
	return out, nil
}

// buildFault 构建创建数据现场
func buildFault(in *model.FaultCreateRequest) *entity.Fault {
	// todo: check the entity is required
	return &entity.Fault{

		UserId: in.UserId,

		EbikeId: in.EbikeId,

		EbikeName: in.EbikeName,

		EbikeTypeId: in.EbikeTypeId,

		EbikeTypeName: in.EbikeTypeName,

		Phone: in.Phone,

		FaultId: in.FaultId,

		FaultName: in.FaultName,

		FaultDesc: in.FaultDesc,

		FaultEnum: in.FaultEnum,

		Level: in.Level,

		SnCode: in.SnCode,

		CreatedAt: time.Now().Unix(),

		UpdatedAt: time.Now().Unix(),
	}
}
